<?php

/*
 * 生成推荐码
 * @param string $str 混淆字符串。用于降低碰撞概率。可写入诸如用户id之类的信息
 * @return string 返回长度为8的推荐码
 */

function make_recommend_code($str = '') {
	$code = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~!@#$%^&*()_+|<>/[]{};"';
	$rand = $code[mt_rand(0, count($code)-1)]
			. strtoupper(dechex(date('m')))
			. date('d') . substr(time(), -5)
			. substr(microtime(), 2, 5)
			. sprintf('%02d', rand(0, 99));
	for (
	$a = md5($rand . $str, true),
	$s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
	$d = '',
	$f = 0; $f < 8; $f++) {
		$g = ord($a[$f]);
		if(mt_rand(0, 1)){
			$d .= $s[( $g ^ ord($a[$f + 8]) ) - $g & 0x1F];
		}else{
			$d .= strtolower($s[( $g ^ ord($a[$f + 8]) ) - $g & 0x1F]);
		}
	}
	return $d;
}
